package com.jf.exam.controller.manage;


import com.jf.exam.pojo.vo.ManageVO;
import com.jf.exam.service.ManageService;
import com.jf.exam.utils.Result;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class ManageInfoController {
    @Autowired
    ManageService manageService;

    @RequestMapping("/password")
    public String password() {
        return "admin/password";
    }

    /**
     * 检查密码是正确
     *
     * @param password 密码
     * @param session  session
     */
    @RequestMapping("/password/check")
    @ResponseBody
    public Result check(String password, HttpSession session) {
        ManageVO manager = (ManageVO) session.getAttribute("manager");
        Md5Hash md5Hash = new Md5Hash(password, manager.getName());
        if (manager.getPassword().equals(md5Hash.toString())) {
            return new Result(Result.CODE_SUCCESS, "校验密码成功");
        }
        return new Result(Result.CODE_FAILURE, "校验密码失败");
    }

    public void aaa() {
        System.out.println("aaa");
    }

    /**
     * 修改密码
     */
    @RequestMapping("/password/modify")
    public String modify(String oldPassword, String newPassword, String newPasswordConfirm, HttpSession session, Model model) {
        ManageVO manager = (ManageVO) session.getAttribute("manager");
        Md5Hash md5Hash = new Md5Hash(oldPassword, manager.getName());
        if (!manager.getPassword().equals(md5Hash.toString())) {
            return "error";
        }
        manager.setModified(true);
        manager.setPassword(new Md5Hash(newPassword, manager.getName()).toString());
        manageService.updatePassword(manager);
        model.addAttribute("message", "修改成功");
        model.addAttribute("url", "/admin/index");
        return "success";
    }


}
